import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.net.*;

public class Exp {
    public static void main(String args[]) {
        File src = new File("src.txt");
        File dest = new File("modified.txt");
        Scanner cin = new Scanner((System.in));
        System.out.println("Enter the string to be replaced.");
        System.out.print(">");
        String replaced = cin.nextLine();
        System.out.println("Enter the target string.");
        System.out.print(">");
        String target = cin.nextLine();
        if (!src.exists()) {
            System.out.println("Error: Source file does not exist.");
            return;
        }
        try (Scanner in = new Scanner(src);
             PrintWriter out = new PrintWriter(dest);) {
            while (in.hasNext()) {
                StringBuilder line = new StringBuilder(in.nextLine());
                int current = 0;
                while (current <= line.length() - 1) {
                    current = line.indexOf(replaced, current);
                    if (current >= 0) {
                        line.replace(current, current + replaced.length(), target);
                        current += target.length();
                    }
                    else break;
                }
                out.println(line);
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println("The strings have been replaced successfully.");
    }
}